package com.fate.system.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fate.framework.dto.IdParamDTO;
import com.fate.framework.utils.SystemUtil;
import com.fate.system.dto.menu.*;
import com.fate.system.entity.Menu;
import com.fate.system.service.IMenuService;
import com.fate.system.vo.menu.MenuDetailVO;
import com.fate.system.vo.menu.MenuListVO;
import com.fate.system.vo.menu.MenuTreeVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.List;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "菜单操作 API")
@RestController
@RequestMapping("/api/v1.0/menu")
public class MenuController {

  @Resource private IMenuService menuService;

  @SaCheckRole("super_admin")
  @Operation(summary = "新增")
  @PostMapping("/add")
  public void add(@RequestBody @Valid MenuAddDTO addDTO) {
    Menu menu = SystemUtil.buildResource(addDTO, Menu.class);
    menuService.save(menu);
  }

  @SaCheckRole("super_admin")
  @Operation(summary = "删除")
  @PostMapping("/remove")
  public void remove(@RequestBody @Valid IdParamDTO idParamDTO) {
    menuService.removeById(idParamDTO.getId());
  }

  @SaCheckRole("super_admin")
  @Operation(summary = "更新")
  @PostMapping("/update")
  public void update(@RequestBody @Valid MenuUpdateDTO updateDTO) {
    Menu menu = SystemUtil.buildResource(updateDTO, Menu.class);
    menuService.updateById(menu);
  }

  @SaCheckRole("super_admin")
  @Operation(summary = "详情")
  @PostMapping("/detail")
  public MenuDetailVO detail(@RequestBody @Valid IdParamDTO idParamDTO) {
    Menu menu = menuService.getById(idParamDTO.getId());
    return SystemUtil.buildResource(menu, MenuDetailVO.class);
  }

  @SaCheckRole("super_admin")
  @Operation(summary = "分页列表")
  @PostMapping("/getPageList")
  public Page<MenuListVO> getPageList(@RequestBody @Valid MenuPageDTO pageDTO) {
    Page<Menu> page = menuService.getPageList(pageDTO);
    return SystemUtil.buildResource(page, MenuListVO.class);
  }

  @SaCheckRole("super_admin")
  @Operation(summary = "获取菜单树")
  @PostMapping("/getMenuTree")
  public List<MenuTreeVO> getMenuTree(@RequestBody @Valid MenuTreeDTO menuTreeDTO) {
    return menuService.getMenuTree(menuTreeDTO);
  }

  @SaCheckRole("super_admin")
  @Operation(summary = "权限分配")
  @PostMapping("/permissionAssignment")
  public void permissionAssignment(
      @RequestBody @Valid PermissionAssignmentDTO permissionAssignmentDTO) {
    menuService.permissionAssignment(permissionAssignmentDTO);
  }

  @SaCheckRole("super_admin")
  @Operation(summary = "根据角色id获取权限列表")
  @PostMapping("/getPermissionList")
  public List<String> getPermissionList(@RequestBody @Valid IdParamDTO idParamDTO) {
    return menuService.getPermissionList(idParamDTO.getId());
  }
}
